#include "format_conversion.h"

/**
 * @brief 字符转大写
 *
 * @param c     - 字符
 * @return char - 转换后的字符
 */
static char lowtocap(char c);

static char lowtocap(char c)
{
  if ((c >= 'a') && (c <= 'z'))
  {
    c = c - 32;
  }

  return c;
}

void CharToHex(char *src, uint8_t *dest, int len)
{
  int dh, dl;
  char ch, cl;
  int i;

  if ((src == NULL) || (dest == NULL) || (len < 1))
    return;

  for (i = 0; i < len; i++)
  {
    ch = src[2 * i];
    cl = src[2 * i + 1];
    dh = lowtocap(ch) - '0';
    if (dh > 9)
    {
      dh = lowtocap(ch) - 'A' + 10;
    }

    dl = lowtocap(cl) - '0';
    if (dl > 9)
    {
      dl = lowtocap(cl) - 'A' + 10;
    }

    dest[i] = dh * 16 + dl;
  }
}

void HexToString(uint8_t *src, char *dest, uint16_t len)
{
  char dh, dl;

  if (((src == NULL) || (dest == NULL)) && (len != 0))
    return;

  for (uint16_t i = 0; i < len; i++)
  {
    dh = '0' + src[i] / 16;
    dl = '0' + src[i] % 16;

    if (dh > '9')
    {
      dh = dh - '9' - 1 + 'A';
    }

    if (dl > '9')
    {
      dl = dl - '9' - 1 + 'A';
    }

    dest[2 * i] = dh;
    dest[2 * i + 1] = dl;
  }
}
